package org.lmind.srd35.world

import java.util.HashMap
import java.util.ArrayList

class World {
  
  var _round = 0

  var _existenceList = new ArrayList[Existence]()
  var _existenceMap = new HashMap[(Int, Int), Square]()
  
  def ground(position: (Int, Int)) = {
    val g = new Ground()
    g.position = position
    g.world = this
    g
  }

  def aroundOf(position: (Int, Int), distance: Int) = {
    var list = List[Existence]()
    for (val i <- position._1 - distance to position._1 + distance) {
      for (val j <- position._2 - distance to position._2 + distance) {
        for (val ex <- _existenceMap.get((i, j)).existence) {
          if (ex.distanceOf(position) <= distance)
            list = ex :: list
        }
      }
    }
    list.reverse
  }

  def aroundOf(e: Existence, distance: Int): List[Existence] = aroundOf(e.position, distance)
  
  def affect(source:Existence, target:Existence, action:Effect) = {
    val s = source
    val t = target
    val context = new EffectContext {
      def world = World.this
      def source = s
      def target = t
    }
    action.doAction(context)
  }
}